package io.renren.task;
import io.renren.entity.LinePasswordEntity;
import io.renren.entity.LineQcodeEntity;
import io.renren.entity.ShopOrderEntity;
import io.renren.entity.ShopOrderVOEntity;
import io.renren.service.LinePasswordService;
import io.renren.service.LineQcodeService;
import io.renren.service.ShopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.*;

@Service
public class SchedulerTask {

    @Autowired
    ShopOrderService shopOrderService;

    @Autowired
    LineQcodeService lineQcodeService;

    @Autowired
    LinePasswordService linePasswordService;

    /**
     * 每隔五秒执行一次
     */
    @Scheduled(cron = "1/5 * * * * *")
    public void task1(){
        /**
         * 系统自动结束超时的订单
         */
        if(shopOrderService!=null){
            List<ShopOrderEntity> list = shopOrderService.getOrderList();
            if(list!=null && list.size()>0){
                for(ShopOrderEntity entity:list){
                    if(entity.getEndtime().getTime()-new Date().getTime()<=0){
                        entity.setOrderStatus(1);
                        shopOrderService.saveOrUpdate(entity);

                        //订单结束后密码置空
                        ShopOrderVOEntity shopOrderVOEntity = shopOrderService.getOrderByOrderSn(entity.getOrderSn());
                        if(shopOrderVOEntity !=null){
                            LineQcodeEntity lineQcodeEntity = lineQcodeService.getCodeByCodeSn(shopOrderVOEntity.getQcode());
                            if(lineQcodeEntity !=null){
                                LinePasswordEntity linePasswordEntity = linePasswordService.getByQCodeId(lineQcodeEntity.getId());
                                linePasswordEntity.setPassword("");
                                linePasswordService.saveOrUpdate(linePasswordEntity);
                            }
                        }
                    }

                }
            }
        }
    }
}
